package com.mine.modules.vm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mine.common.domain.vo.Result;
import com.mine.modules.vm.domain.dto.AssetsPersonnelDto;
import com.mine.modules.vm.domain.vo.Personnel;
import com.mine.modules.vm.entity.VmAssets;
import com.mine.modules.vm.entity.VmAssetsPersonnel;
import com.mine.modules.vm.entity.VmPersonnel;
import com.mine.modules.vm.entity.VmPersonnelDepart;
import com.mine.modules.vm.mapper.VmAssetsPersonnelMapper;
import com.mine.modules.vm.service.IVmAssetsPersonnelService;
import com.mine.modules.vm.service.IVmAssetsService;
import com.mine.modules.vm.service.IVmPersonnelDepartService;
import com.mine.modules.vm.service.IVmPersonnelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xujf
 * @since 2024-12-24
 */
@Service
public class VmAssetsPersonnelServiceImpl extends ServiceImpl<VmAssetsPersonnelMapper, VmAssetsPersonnel> implements IVmAssetsPersonnelService {

    @Resource
    private IVmAssetsService vmAssetsService;

    @Resource
    private IVmPersonnelService vmPersonnelService;

    @Resource
    private IVmPersonnelDepartService vmPersonnelDepartService;

    @Override
    public Result<?> getPersonnelIdsByDeviceId(String deviceId, String personnelType) {
        List<String> ids = this.lambdaQuery()
                .eq(VmAssetsPersonnel::getAssetsId, deviceId)
                .eq(StringUtils.isNotBlank(personnelType), VmAssetsPersonnel::getPersonnelClassify, personnelType)
                .list()
                .stream()
                .map(VmAssetsPersonnel::getPersonnelId)
                .collect(Collectors.toList());
        if(ids.isEmpty()){
            return Result.ok(Collections.EMPTY_LIST);
        }
        List<VmPersonnel> users = vmPersonnelService.lambdaQuery().in(VmPersonnel::getId, ids).list();
        List<Personnel> res = new ArrayList<>();
        for (VmPersonnel user : users) {
            VmPersonnelDepart dept = vmPersonnelDepartService.getById(user.getDepartmentId());
            res.add(new Personnel(
                    user.getId(),
                    user.getDepartmentId(),
                    user.getUsername(),
                    user.getRealName(),
                    dept != null ? dept.getDepartmentName() : ""
            ));
        }
        return Result.OK(res);
    }

    @Override
    public Result<?> batchBindingPersonnelByDeviceId(AssetsPersonnelDto assetsPersonnel) {

        if (assetsPersonnel == null) {
            return Result.error("参数必填！");
        }
        if (StringUtils.isBlank(assetsPersonnel.getDeviceId()) || StringUtils.isBlank(assetsPersonnel.getPersonnelType())) {
            return Result.error("参数不完整！");
        }
        VmAssets vmAssets = vmAssetsService.getById(assetsPersonnel.getDeviceId());
        if (vmAssets == null) {
            return Result.error("设备不存在，请刷新重试！");
        }
        String[] ids = assetsPersonnel.getPersonnelIds().split(",");
        this.lambdaUpdate()
                .eq(VmAssetsPersonnel::getAssetsId, assetsPersonnel.getDeviceId())
                .eq(VmAssetsPersonnel::getPersonnelClassify, assetsPersonnel.getPersonnelType())
                .remove();
        for (String id : ids) {
            this.save(new VmAssetsPersonnel(assetsPersonnel.getDeviceId(), id, assetsPersonnel.getPersonnelType()));
        }
        return Result.ok("操作成功!");
    }

}
